$(function () {

    // 接收上个页面的参数
    var param = location.search;
    // 获取工作详情
    $.ajax({
        type: "get",
        url: "/work/getJob" + param,
        dataType: "json",
        success: function (result) {
            dealResult(result);
        },
        error: function () {
            $("#warning-fade").html("服务请求失败！");
            $("#myModal").modal();
        }
    });

    // 页面赋值
    function dealResult(result) {
        var data = result.data;
        $("#job-id").html(data.jobId);
        $("#job-title").html(data.jobTitle);
        var jobDescribe = data.jobDescribe;
        var jobDescribe_p = jobDescribe.split("\n");
        var desc_html = '';
        jobDescribe_p.forEach(function(element) {
            var html = '<p class="text-muted mb-3">'+element+'</p>';
            desc_html = desc_html + html;
        });
        $("#job-describe").append(desc_html);

        var requirement = data.requirement;
        var requirement_p = requirement.split("\n");
        var requ_html = '';
        requirement_p.forEach(function(element) {
            var html = '<div class="job-details-desc-item">' +
                '<div class="float-left mr-3">' +
                '<i class="mdi mdi-send text-primary"></i>' +
                '</div>' +
                '<p class="text-muted mb-2">'+element+'</p>' +
                '</div>';
            requ_html = requ_html + html;
        });
        $("#job-requirement").append(requ_html);
        $("#department").html("：" + data.department);
        $("#email").html("：" + data.email);
        $("#phone-number").html("：" + data.phoneNumber);
        $("#gmt-create").html("：" + data.gmtCreate);
        $("#salary").html("：￥" + data.hourlyWage + "/小时");
    }

    // 申请工作
    $("#apply-button").click(function () {
        var userId = Cookies.get("userId");
        var jobId = $("#job-id").html();
        $.ajax({
            type: "post",
            url: "/work/applyJob",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({
                "applyUserId": userId,
                "jobId": jobId,
                "applyStatus": 1
            }),
            success: function (result) {
                $("#warning-fade").html(result.statusInfo);
                $("#myModal").modal();
            },
            error: function () {
                $("#warning-fade").html("服务请求失败！");
                $("#myModal").modal();
            }
        });
    });
});